jQuery(function(){
  jQuery('a[href="#formulaire"]').click(function(event){
    jQuery('#formulaire').dialog({
      autoOpen: true,
      height: "auto",
      width: 540,
      modal: true,
      buttons : {
        "Envoyer" : function(){
          var focusTb=[];
          
          var $this = jQuery(this);
          
          $this.find('form li').removeClass('err');
          
          if(jQuery(':input[name="yaMail"]').val()=="" && jQuery(':input[name="yaPhone"]').val()==""){
            focusTb.push(jQuery(':input[name="yaMail"]'));
            focusTb.push(jQuery(':input[name="yaPhone"]'));
          }
          
          if(jQuery(':input[name="yaName"]').val()==""){
            focusTb.push(jQuery(':input[name="yaName"]'));
          }
          
          var text = jQuery(':input[name="yaText"]').val();
          text = text.replace(/\s/g,'');
          if(text.length==0){
            
            focusTb.push(jQuery(':input[name="yaText"]').val(''));
          }
          
          if(focusTb.length>0){
            jQuery.each(focusTb, function(idx,$el){
              $el.closest('li').addClass('err');
              if(idx==0){
                $el.focus();  
              }
            });
                        
            return false;
          }
          
          
          
          var post = com.lolita.utils.serializeFormObject($this.find('form'));
            
          post = jQuery.extend({},{'action' : 'send','what' : 'contact'},post);
        
          jQuery.post(com.lolita.get.ajaxUrl(),post,function(rep){
            if  (console)  console.log(rep);
          });
          
          
        }
      } 
    });
    return com.lolita.utils.cancelEvent(event);
  });
  
  jQuery('form input, form textarea').each(function(){
    var $this = jQuery(this);
    var text = $this.prev().text();
    $this.attr('placeholder',text);
    if($this.is('textarea')){
      $this.attr('placeholder','Quelques mots d\'amour');
    }
    $this.closest('li').addClass('field');
  });
  
  jQuery('form').delegate(':input','focus blur',function(event){
    var $this = jQuery(this);
    if(event.type=='focusin'){
      $this.prev().addClass('ui-state-focus').parent().addClass('focus');
    } else {
      $this.prev().removeClass('ui-state-focus').parent().removeClass('focus');
    }
  });
});

